import math
import numpy as np
import matplotlib.pyplot as plt
from itertools import permutations, combinations
from collections import Counter
import scipy.stats as stats

from mpl_toolkits.mplot3d import Axes3D


def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()

def solid_geometry_and_vectors():
    print("=== 立体几何与向量 ===")
    
    # 向量运算
    v1 = np.array([1, 2, 3])
    v2 = np.array([4, 5, 6])
    
    print(f"向量 v1 = {v1}")
    print(f"向量 v2 = {v2}")
    print(f"向量加法: v1 + v2 = {v1 + v2}")
    print(f"向量点积: v1 · v2 = {np.dot(v1, v2)}")
    print(f"向量叉积: v1 × v2 = {np.cross(v1, v2)}")
    print(f"向量模长: |v1| = {np.linalg.norm(v1):.2f}")
    
    # 空间几何可视化
    fig = plt.figure(figsize=(15, 5))
    
    # 1. 向量
    ax1 = fig.add_subplot(131, projection='3d')
    
    # 绘制向量
    ax1.quiver(0, 0, 0, v1[0], v1[1], v1[2], color='r', arrow_length_ratio=0.1, label='v1')
    ax1.quiver(0, 0, 0, v2[0], v2[1], v2[2], color='b', arrow_length_ratio=0.1, label='v2')
    ax1.quiver(0, 0, 0, v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2], color='g', arrow_length_ratio=0.1, label='v1+v2')
    
    ax1.set_xlim([0, 6])
    ax1.set_ylim([0, 8])
    ax1.set_zlim([0, 10])
    ax1.set_xlabel('X')
    ax1.set_ylabel('Y')
    ax1.set_zlabel('Z')
    ax1.legend()
    ax1.set_title('向量运算')
    
    # 2. 平面
    ax2 = fig.add_subplot(132, projection='3d')
    
    # 平面方程: 2x + 3y + 4z = 12
    xx, yy = np.meshgrid(range(-2, 3), range(-2, 3))
    zz = (12 - 2*xx - 3*yy) / 4
    
    ax2.plot_surface(xx, yy, zz, alpha=0.7, color='cyan')
    ax2.set_xlabel('X')
    ax2.set_ylabel('Y')
    ax2.set_zlabel('Z')
    ax2.set_title('平面: 2x + 3y + 4z = 12')
    
    # 3. 球体
    ax3 = fig.add_subplot(133, projection='3d')
    
    u = np.linspace(0, 2 * np.pi, 30)
    v = np.linspace(0, np.pi, 30)
    x = np.outer(np.cos(u), np.sin(v))
    y = np.outer(np.sin(u), np.sin(v))
    z = np.outer(np.ones(np.size(u)), np.cos(v))
    
    ax3.plot_surface(x, y, z, color='lightcoral', alpha=0.7)
    ax3.set_xlabel('X')
    ax3.set_ylabel('Y')
    ax3.set_zlabel('Z')
    ax3.set_title('球体')
    
    plt.tight_layout()
    plt.show()

solid_geometry_and_vectors()